package com.huarui.sale.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.huarui.sale.dao.IExpressItemDao;
import com.huarui.sale.model.ExpressItem;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class ExpressItemDao extends BaseDaoImpl implements IExpressItemDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getExpressItemForMapByExpressIds(List<Integer> ids) {
	
		return getSqlMapClientTemplate().queryForList(namespace+".getExpressItemForMapByExpressIds", ids);
	}
	

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getExpressItemForMapByExpressId(Integer expressId) {
		return  getSqlMapClientTemplate().queryForList(namespace+".getExpressItemForMapByExpressId", expressId);
	}

	@Override
	public void batchInsertExpressItem(final List<ExpressItem> expressItems) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				int batch = 0;
				for(ExpressItem expressItem : expressItems){
					executor.insert(namespace+".create", expressItem);
					batch++;
					if(batch == 50){
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});
	}

	@Override
	public void batchUpdateExpressItem(final List<ExpressItem> expressItems) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				int batch = 0;
				for(ExpressItem expressItem : expressItems){
					executor.update(namespace+".update", expressItem);
					batch++;
					if(batch == 50){
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public void bacthDeleteExpressItemByIds(List<Map> expressItemDeletedIds) {
		for(Map map : expressItemDeletedIds){
			getSqlMapClientTemplate().delete(namespace+".deleteById", map);
		}
		
	}


	@Override
	public void batchUpdateExpressItemInventoryId(final List<ExpressItem> expressItems) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				for(ExpressItem expressItem : expressItems){
					executor.update(namespace+".updateExpressItemInventoryId", expressItem);
				}
				executor.executeBatch();
				return null;
			}
		});		
	}


	@SuppressWarnings("unchecked")
	@Override
	public Integer getExpressItemCountByConditions(Map queryMap) {
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace+".getExpressItemCountByConditions", queryMap);
		 
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map> getExpressItemForMapByConditions(Map queryMap) {
		return getSqlMapClientTemplate().queryForList(namespace+".getExpressItemForMapByConditions", queryMap);
	}

	
}
